<div class="row animate fadeIn">
    <form class="form-horizontal" id="sub_inv_form">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        
            <div class="form-group">
                <label class="col-lg-3 control-label">Sub-Inventory Code</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" id="sub_code" name="invSub_code" value ="">
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">Sub-Inventory Name</label>
                <div class="col-lg-4">
                    <input type="text" id="sub_name"class="form-control" name="invSub_name">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Description</label>
                <div class="col-lg-4">
                    <input type="text" id="sub_desc" class="form-control" name="invSub_desc">
                </div>
            </div>
      </div>
        </form>
        <div id="add_locator_sched" class="ibox-content">
            <h3 style="color: #00afef">Create Locator</h3>
              <form  id="sub_locator_form" class="form-inline">
                <input type="hidden" name="hidden_sub_id" id="s_sub_id">
            </br>
              <table id="pgTable"class="table table-bordered" style="width:80%;">
                <thead>
                    <tr>
                      <th style="">Locator Code</th>
                      <th style="">Description</th>
                      <th style="width:50px;">Action</th>
                    </tr>
                    <tr>
                      <td style="margin:0px;padding:0px;">
                          <input style="width:100%;" type="text" id="" name="locate_code" class="form-control-custom" value="">
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <input style="width:100%;" type="text" id="" name="locate_desc" class="form-control-custom">
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <button style="width:100%;" class="btn btn-white" id=""><i class="fa fa-plus success"></i></button>
                      </td>
                    </tr>      
                </thead>
              </form>
                <tbody id="tbl_locatorAdd" class="animate fadeIn">
                                                
                </tbody>
              </table> 
        </div>
    
 </div>
 <script>
    $(document).ready(function(){
    $('#sub_inv_form').data('serialize',$('#sub_inv_form').serialize());
    $('#add_locator_sched').hide();
    $('#sub_inv_form').validate({
        rules :{
            invSub_code:{
                required: true
            },
            invSub_name:{
                required: true
            }
        },submitHandler: function(form){
          dataString = $("#sub_inv_form").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/insert_sub_inv'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .New Sub-Inventory Added Successfully");
                mID = data[0].id; console.log(mID);
                $('#s_sub_id').val(mID);
                $('#add_locator_sched').show();
                $('#sub_inv_form').find('input, button').attr('disabled','disabled');
                $('#cancel_add').html('Complete');
            }
          }      
         });   
        }
      });



      $('#sub_locator_form').validate({
        rules :{
            locate_code:{
                required: true
            }
        },submitHandler: function(form){
          dataString = $("#sub_locator_form").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/insert_sub_inv_locator'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .Sub-Inventory Locator Added Successfully");
                $('#tbl_locatorAdd').empty();
                var trHTML = '';
                $.each(data, function(i, item) {
                    trHTML += '<tr><td text-align:right;>' + data[i].locator_code + '</td><td text-align:right;>' + data[i].locator_description + '</td><td></td></tr>';
                });
                $('#tbl_locatorAdd').append(trHTML);
                $('#sub_locator_form')[0].reset();
            }   
          }   
         });   
        }
      });




      function alert_cancel(){
            if($('#sub_inv_form').serialize()!=$('#sub_inv_form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_add(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_add(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_add').click(function () {
          if($('#cancel_add').html() == "Complete"){
            close_add(); active_search();
            notifymsg("hide","hide");
          }else{            
            alert_cancel();
          }
        });


      $('#addX').click(function(){
            alert_cancel();      
       });
    });
 </script>  